package cn.cookyfly.dailyreview.server.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

public class DbUtil {
	
	private static final Logger logger = Logger.getLogger(DbUtil.class.getName());

	public static Connection getNewConnection() throws SQLException {
		String defaultDb = PropertiesDef.getDefaultDatabase();
		if (defaultDb == null) {
			logger.severe("Getting DB connection failed due to: PropertiesDef.getDefaultDatabase() returned " +
					"null! Please specify the default database name in the properties file, or the system might not " +
					"act like expected.");
			return null;
		}
		return getNewConnection(defaultDb);
	}

	public static Connection getNewConnection(String database) throws SQLException {
		if (StringUtils.isBlank(database)) {
			throw new IllegalArgumentException("database can not be a blank string");
		}
		String connStr = "jdbc:sqlite:" + database;
		Connection con = null;
		con = DriverManager.getConnection(connStr);
		return con;
	}

}
